/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.imgs;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * <p>Project: javaseapp - Img4
 * <p>Powered by webrx On 2021-08-13 11:35:35
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 16
 */
public class Img5 {
    public static void main(String[] args) {
        String src = "images/abc.png";
        try {
            BufferedImage i = ImageIO.read(new File(src));
            int w = i.getWidth();
            int h = i.getHeight();
            var g = i.createGraphics();
            //设置图像中文本，线程的平滑
            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);

            g.drawImage(i, 0, 0, null);
            //设置文字颜色
            Color c = new Color(0, 0, 0, 215);
            g.setColor(c);

            //设置文字的字体
            Font font = new Font("微软雅黑", Font.BOLD, 35);
            g.setFont(font);

            String text = "联系我们：13014577033";
            FontMetrics fm = g.getFontMetrics();
            int tw = fm.stringWidth(text);
            int th = fm.getHeight();
            int ascent = fm.getAscent();
            int descent = fm.getDescent();

            //左上角
            float x = 0f;
            float y = ascent - descent / 2;

            //右下角
            x = w - tw;
            y = h - descent / 2;

            //正中央
            x = (w - tw) / 2;
            y = (h - th) / 2 + ascent;

            //下边 水平中央位置
            //x = (w - tw) / 2;
            //y = h - descent / 2 - 10;

            g.drawString(text, x, y);

            ImageIO.write(i, "png", new File("images/abc_text.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
